package com.chalk.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.chalk.common.constant.TypeConstants;
import com.chalk.config.security.SecurityPasswordUtil;
import com.chalk.dao.MemberDao;
import com.chalk.model.Member;

import com.chalk.model.SysUser;

import com.chalk.service.MemberService;
import com.chalk.vo.MemberVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 会员表(Member)表服务实现类
 *
 * @author maxiaoqiang
 * @since 2019-03-22 17:38:23
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberDao, Member> implements MemberService {

    @Autowired
    private MemberDao memberDao;

    /*自定义接口*/

    /**
     * 根据主键批量(逻辑)删除
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteBatchByPrimaryKey(String[] ids) {
        return memberDao.deleteBatchByPrimaryKey(ids);
    }

    @Override
    public Page<Member> selectFanPage(Page page, MemberVo member) {
        return page.setRecords(this.memberDao.selectFanPage(page, member));
    }

//    @Transactional
//    @Override
//    public boolean updateCustomById(Member member) {
//        DateTime date = DateUtil.date();
//        Member temp = super.selectById(member.getMemberId());
//        if ("0".equals(member.getType())) {
//            member.setIntegral((temp.getIntegral() + member.getTempIntegral()));
//        } else if ("1".equals(member.getType())) {
//            member.setIntegral((temp.getIntegral() - member.getTempIntegral()));
//        }
//        member.setUpdateTime(date);
//        super.updateById(member);
//        return this.modifyPointsRecord(member);
//    }


//    private boolean modifyPointsRecord(Member member){
//        // 抽奖积分消耗记录添加
//        MemberPointsRecord memberPointsRecord = new MemberPointsRecord();
//        memberPointsRecord.setMemberId(member.getMemberId());
//        memberPointsRecord.setPointsValue(member.getTempIntegral());
//        memberPointsRecord.setBusinessType(TypeConstants.MemberPointsRecordBusinessType.SYSTEM_MODIFY.getValue());
//        if ("0".equals(member.getType())) {
//            memberPointsRecord.setOpType(TypeConstants.MemberPointsRecordOpType.ADD.getValue());
//        } else if ("1".equals(member.getType())) {
//            memberPointsRecord.setOpType(TypeConstants.MemberPointsRecordOpType.DELETE.getValue());
//        }
//        memberPointsRecord.setPointsType(1);
//        memberPointsRecord.setBusinessData(TypeConstants.MemberPointsRecordBusinessType.SYSTEM_MODIFY.getName());
//        memberPointsRecord.setMemo(TypeConstants.MemberPointsRecordBusinessType.SYSTEM_MODIFY.getName());
//        memberPointsRecord.setCreateTime(member.getUpdateTime());
//        memberPointsRecord.setUpdateTime(member.getUpdateTime());
//        return memberPointsRecordService.insert(memberPointsRecord);
//    }

    @Override
    public List<String> getShopIdsByQusId(String memberId) {
        //获取分销员列表
        List<Member> members=selectList(new EntityWrapper<Member>().eq("distributor_id",2).eq("reference_id",memberId));

        List<String> shopIds=new ArrayList<>();
        //获取门店列表
        for(Member m:members){

            List<Member> shops=selectList(new EntityWrapper<Member>().eq("member_type",3).eq("reference_id",m.getMemberId()));

            for(Member shop:shops){
                shopIds.add(shop.getNewShopId());
            }

        }


        return shopIds;
    }

    @Override
    public Boolean resetPassword(String memberId) {
        Member member=new Member();
        member.setMemberId(memberId);
        member.setPassword(SecurityPasswordUtil.md5Encrypt("123456"));
        member.setUpdateTime(new Date());
        Boolean f=updateById(member);
        return f;
    }

//    @Override
//    public Member getMemberForShop(SysUser sysUser) {
//        //第一种
//        if(StringUtils.isNotBlank(sysUser.getMemberId())) {
//            Member member = selectById(sysUser.getMemberId());
//            return member;
//        }else {
//            Member member=selectOne(new EntityWrapper<Member>().eq("shop_id",sysUser.getShopId()).eq("member_type",3));
//            return member;
//        }
//    }
}